package com.learn.springbootnetty.config;

import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.FormHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.servlet.config.annotation.*;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

/**
 * 基础入口配置
 */
@Configuration
@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {


    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**").
                allowedOriginPatterns("*").
                allowedMethods("POST", "GET", "OPTIONS").
                maxAge(3600).
                allowCredentials(true);
    }


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        List<String> excludePathPatterns = new ArrayList<>();
        excludePathPatterns.add("/auth/verificationCode");
        excludePathPatterns.add("/auth/login");
        excludePathPatterns.add("/weixin/*/*");
        excludePathPatterns.add("/weixin/*");
        excludePathPatterns.add("/auth/logout");
        excludePathPatterns.add("/sysLoginManage/sendVerCode");


       /* registry.addInterceptor(si)
               // 设置要过滤的内容
               .addPathPatterns("/**")
                .excludePathPatterns(excludePathPatterns)
                .excludePathPatterns(securePath);

        registry.addInterceptor(resourceInterceptor)
                // 设置要过滤的内容
                .addPathPatterns("/**")
                .excludePathPatterns(excludePathPatterns)
                .excludePathPatterns(securePath);*/
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        StringHttpMessageConverter converter = new StringHttpMessageConverter(Charset.forName("UTF-8"));
        FormHttpMessageConverter converter1 = new FormHttpMessageConverter();
        converter1.setCharset(Charset.forName("UTF-8"));
        FastJsonHttpMessageConverter converter2 = new FastJsonHttpMessageConverter();
        converter2.setDefaultCharset(Charset.forName("UTF-8"));
        converters.add(converter);
        converters.add(converter1);
        converters.add(converter2);
    }



    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations(
                "classpath:/static/");
    }
}
